﻿using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WinFormsApp1.Common;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace WinFormsApp1.TongShiWei
{
    public partial class AddScaleContext : UIForm
    {
        private DataGridViewComboBoxColumn comboBoxColumn;
        public AddScaleContext()
        {
            InitializeComponent();
            //获取仓库类型表数据
            GetDepotTypeData();
            //获取磅秤分类表数据
            GetScaleTypeData();
            //获取商品代号表数据
            //GetUPCData();
            //获取商品代号下拉框数据
            GetUPCDataSelect();

        }
        /// <summary>
        /// 获取仓库类型表数据
        /// </summary>
        public async void GetDepotTypeData()
        {
            //http://localhost:5106/api/Tsw/GetDepottypeData
            List<DepotType> data = new List<DepotType>()
            {
                new DepotType{ DepotTypeId=0,DepotTypeName="请选择仓库"}
            };
            var res = await HttpClientHelper.RequestUrl<DepotType, ApiResult<List<DepotType>>>(URLCommon.Url2, "/Tsw/GetDepottypeData", HttpClientHelper.MethodType.GET);
            if (res.Code > 0)
            {
                data.AddRange(res.Data);
                BindDepotId.DataSource = data;
                BindDepotId.DisplayMember = "DepotTypeName";
                BindDepotId.ValueMember = "DepotTypeId";
            }
        }

        /// <summary>
        /// 获取磅秤类型表数据
        /// </summary>
        public async void GetScaleTypeData()
        {
            //http://localhost:5106/api/Tsw/GetScaleTypeData
            List<ScaleType> data = new List<ScaleType>()
            {
                new ScaleType{ ScaleTypeId=0,ScaleTypeName="请选择磅秤类型"}
            };
            var res = await HttpClientHelper.RequestUrl<ScaleType, ApiResult<List<ScaleType>>>(URLCommon.Url2, "/Tsw/GetScaleTypeData", HttpClientHelper.MethodType.GET);
            if (res.Code > 0)
            {
                data.AddRange(res.Data);
                ScaleTypeId.DataSource = data;
                ScaleTypeId.DisplayMember = "ScaleTypeName";
                ScaleTypeId.ValueMember = "ScaleTypeId";
            }
        }

        #region 获取商品代号
        public async void GetUPCData()
        {
            //http://localhost:5106/api/Tsw/GetUPCData
            GetUPCContext UPCData = new GetUPCContext();
            var res = await HttpClientHelper.RequestUrl<GetUPCContext, ApiResult<List<ScaleType>>>(URLCommon.Url2, "/Tsw/GetUPCData", HttpClientHelper.MethodType.POST, UPCData);
            if (res.Code > 0)
            {

            }
        }
        public class GetUPCContext
        {
            public int GoodsMinuteId { get; set; } = 0;
            public string UPCDataName { get; set; } = string.Empty;
        };
        #endregion

        /// <summary>
        /// 获取商品代号下拉框
        /// </summary>
        List<UPCData> data = new List<UPCData>();
        public async void GetUPCDataSelect()
        {
            GetUPCContext GetUPCData = new GetUPCContext();
            //给磅秤表中的表格添加第一行数据
            //WeighGoods.Columns.Add("序号", "序号");
            //WeighGoods.Columns.Add("顺序号", "顺序号");
            //添加磅秤表中的表格添加下拉框
            //this.comboBoxColumn = new DataGridViewComboBoxColumn();
            // comboBoxColumn.HeaderText = "商品代号";
            // 设置下拉选项

            //http://localhost:5106/api/Tsw/GetUPCData
            var res = await HttpClientHelper.RequestUrl<GetUPCContext, ApiResult<List<UPCData>>>(URLCommon.Url2, "/Tsw/GetUPCData", HttpClientHelper.MethodType.POST, GetUPCData);
            if (res.Code > 0)
            {
                data = res.Data;
                UPCDataName.DataSource = res.Data;
                UPCDataName.DisplayMember = "UPCDataName";
                UPCDataName.ValueMember = "GoodsMinuteId";

            }
            //WeighGoods.Columns.Add(comboBoxColumn);
            //WeighGoods.Columns.Add("商品代号", "商品代号");
            //WeighGoods.Columns.Add("代号ID", "代号ID");
            //WeighGoods.Columns.Add("助记码", "助记码");
            //WeighGoods.Columns.Add("备注", "备注");
            //WeighGoods.CellValueChanged += DataGridView_CellClick;
        }

        #region 添加磅秤表数据
        private async void uiButton2_Click(object sender, EventArgs e)
        {
            //实例化拼接字符串
            StringBuilder GetUPCDataName = new StringBuilder();
            //行数减2的原因是行数的索引是从-1开始的
            var WeighGoodsNum = WeighGoods.Rows.Count - 2;
            var AllUPCName = "";
            for (int i = 0; i <= WeighGoodsNum; i++)
            {
                var UPCIndexNum = WeighGoods.Rows[i].Cells[3].Value.ToString();
                var GetUpcName = data.FirstOrDefault(x => x.GoodsMinuteId == (Convert.ToInt32(UPCIndexNum)));
                //DataGridViewRow row = WeighGoods.Rows[i];
                GetUPCDataName.Append(GetUpcName.UPCDataName + ",");

            }
            //http://localhost:5159/api/TSW/AddScaleData
            ScaleContext UPCIndexData = new ScaleContext()
            {
                ScaleId = 0,
                ScaleTypeId = Convert.ToInt32(ScaleTypeId.SelectedValue),
                ScaleName = ScaleName.Text,
                IsScaleState = IsScaleState.Active,
                TareNum = Convert.ToInt32(TareNum.Text),
                IsTareAdd = IsTareAdd.Active,
                BindDepotId = BindDepotId.SelectedIndex,
                IsGoDepot = IsGoDepot.Active,
                UPCDataName = GetUPCDataName.ToString(),
                OperatorName = OperatorName.Text,
                ScaleCreateTime = DateTime.Now,
                Modifier = "123",
                RevempTime = DateTime.Now,
                IsScaleDelete = false,
                IsScaleDisable = false,
            };
            if (UPCIndexData != null)
            {
                //添加磅秤
                //http://localhost:5159/api/TSW/AddScaleData
                var res = await HttpClientHelper.RequestUrl<ScaleContext, ApiResult<ScaleContext>>(URLCommon.Url1, "/TSW/AddScaleData", HttpClientHelper.MethodType.POST, UPCIndexData);
                if (res.Code > 0)
                {
                    MessageBox.Show("添加成功");
                    ShowScaleContext showScaleContext = new ShowScaleContext();
                    showScaleContext.Show();
                    this.Hide();
                }
                else
                {
                    MessageBox.Show("添加失败");
                }
            }
            else
            {
                MessageBox.Show("添加失败");
            }
        }
        private void uiPanel4_Click(object sender, DataGridViewCellEventArgs e)
        {
            uiButton2_Click(sender, e);
        }
        #endregion

        /// <summary>
        /// 仓库类型表
        /// </summary>
        public class DepotType
        {
            /// <summary>
            /// 仓库类型ID
            /// </summary>
            public int DepotTypeId { get; set; }
            /// <summary>
            /// 仓库类型名称
            /// </summary>
            public string DepotTypeName { get; set; } = string.Empty;
            /// <summary>
            /// 是否删除仓库类型{false=不删除；true=删除}
            /// </summary>
            public bool IsDepotType { get; set; }
        }

        /// <summary>
        /// 磅秤类型分类表
        /// </summary>
        public class ScaleType
        {
            [Key]
            public int ScaleTypeId { get; set; }
            public string ScaleTypeName { get; set; } = string.Empty;
            public bool IsScaleTypeDelete { get; set; } = false;
        }

        /// <summary>
        /// 磅秤设置表
        /// </summary>
        public class ScaleContext
        {
            /// <summary>
            /// 磅秤表ID
            /// </summary>
            public int ScaleId { get; set; }
            /// <summary>
            /// 磅秤类型ID
            /// </summary>
            public int ScaleTypeId { get; set; }
            /// <summary>
            /// 磅秤名称
            /// </summary>
            public string ScaleName { get; set; }
            /// <summary>
            /// 使用状态
            /// </summary>
            public bool IsScaleState { get; set; }
            /// <summary>
            /// 皮重扣减(公斤)
            /// </summary>
            public int TareNum { get; set; }
            /// <summary>
            /// 皮重按件累加
            /// </summary>
            public bool IsTareAdd { get; set; }
            /// <summary>
            /// 绑定仓库
            /// </summary>
            public int BindDepotId { get; set; }
            /// <summary>
            /// 自动出/入库
            /// </summary>
            public bool IsGoDepot { get; set; }
            /// <summary>
            /// 商品代号名称
            /// </summary>
            public string UPCDataName { get; set; }
            /// <summary>
            /// 操作人
            /// </summary>
            public string OperatorName { get; set; }
            /// <summary>
            /// 创建时间
            /// </summary>
            public DateTime ScaleCreateTime { get; set; }
            /// <summary>
            /// 最后修改人
            /// </summary>
            public string Modifier { get; set; }
            /// <summary>
            /// 最后修改时间
            /// </summary>
            public DateTime RevempTime { get; set; }
            /// <summary>
            /// 是否删除{false:不删除,true:删除}
            /// </summary>
            public bool IsScaleDelete { get; set; }
            /// <summary>
            /// 是否禁用{false:不禁用,true:禁用}
            /// </summary>
            public bool IsScaleDisable { get; set; }
        }

        /// <summary>
        /// 商品代号表
        /// </summary>
        public class UPCData
        {
            /// <summary>
            /// 商品代号ID
            /// </summary>
            public int GoodsMinuteId { get; set; }
            /// <summary>
            /// 助记码
            /// </summary>
            public string MemoryNum { get; set; } = string.Empty;
            /// <summary>
            /// 商品分类Id
            /// </summary>
            public int CommodityId { get; set; }
            /// <summary>
            /// 顺序号 
            /// </summary>
            public int OrderNum { get; set; }
            /// <summary>
            /// 等级ID
            /// </summary>
            public int GoodsGradeId { get; set; }
            /// <summary>
            /// 商品代号名称
            /// </summary>
            public string UPCDataName { get; set; } = string.Empty;
            /// <summary>
            /// 是否删除
            /// </summary>
            public bool IsGoodsMinute { get; set; } = false;
        }
        /// <summary>
        /// 下拉框查询反填数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void WeighGoods_CellValueChanged_1(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex >= 0 && e.RowIndex >= 0)
            {
                var comboBoxValue = this.WeighGoods.Rows[e.RowIndex].Cells[e.ColumnIndex].EditedFormattedValue.ToString();
                var UPCData = data.FirstOrDefault(x => x.UPCDataName == comboBoxValue);
                if (UPCData != null)
                {

                    WeighGoods.Rows[e.RowIndex].Cells[0].Value = 1;
                    WeighGoods.Rows[e.RowIndex].Cells[1].Value = UPCData.OrderNum;
                    //WeighGoods.Rows[e.RowIndex].Cells[2].Value = 1;
                    WeighGoods.Rows[e.RowIndex].Cells[3].Value = UPCData.GoodsGradeId;
                    WeighGoods.Rows[e.RowIndex].Cells[4].Value = UPCData.MemoryNum;
                }
            }
            else
            {
                return;
            }
        }
    }
}
